let mongoose = require('mongoose');
let ObjectId = mongoose.Schema.Types.ObjectId;
let crypto = require('crypto');
const conn = mongoose.createConnection('mongodb://localhost:27017/xx',{ useNewUrlParser: true });
let UserSchema = new mongoose.Schema({
    username:{type:String,required:true},
    password:String,
    age:Number,
    phone:String,
    province:String,
    city:String,
    createAt:{type:Date,default:Date.now}
});
UserSchema.pre('save',function(next){
  this.password = crypto.createHmac('sha1','zhufeng').update(this.password).digest('hex');
  next();
});
let User = conn.model('User',UserSchema);
(async function register(username,password){
    //password = crypto.createHmac('sha1','zhufeng').update(password).digest('hex');
    let user = new User({username,password});
    await user.save();
})('11','11');

/* (async function login(username,password){
    password = crypto.createHmac('sha1','zhufeng').update(password).digest('hex');
    let oldUser = await User.findOne({username,password});
    console.log(oldUser);
})('user','pwd'); */